﻿function AzureOnlineQueueMetadataView(context) {

    var self = this;

    var dispatcher = $({});

    var spinner = new SpinnerModel();

    var queue = ko.observable(null);

    var editor = new AzureOnlineMetaEditor(context);
    
    function init(queueModel) {
        queue(queueModel);
        editor.init(queueModel.metadata());
    }

    function submit() {
        var api = context.api();
        if (editor.validate()) {
            var data = editor.getInfo();
            spinner.enter();
            api.queues.setQueueMetadata({
                metadata: data,
                queue: queue().name(),
                success: function() {
                    spinner.leave();
                    queue().metadata(data);
                    dispatcher.trigger('success');
                },
                error: function() {
                    spinner.leave();
                }
            });
        }
    }

    self.queue = queue;
    self.rows = editor.rows;
    self.init = init;
    self.submit = submit;

    self.addOnSuccess = function (handler) {
        dispatcher.bind('success', handler);
    };
}